<template>
  <div class="login_box">
    <el-row>
      <el-col :span="15" :xs="0"></el-col>
      <el-col :span="9" :xs="24">
        <el-form class="login_form">
          <h1>大大呱后台管理系统</h1>
          <el-form-item>
            <el-input
              v-model="loginForm.username"
              size="large"
              placeholder="账号"
              :prefix-icon="User"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="loginForm.password"
              type="password"
              size="large"
              placeholder="密码"
              :prefix-icon="Lock"
              show-password
            />
          </el-form-item>
          <div class="register">
            <p>注册</p>
            <p>忘记密码？</p>
          </div>
          <el-form-item>
            <el-button
              @click="login"
              color="#626aef"
              round
              class="butStyle"
              :loading="loading"
            >
              登&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { Lock, User } from '@element-plus/icons-vue'
import { reactive, h, ref } from 'vue'
//引入用户相关的小仓库
import useUserStore from '@/store/moudules/user'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'

let userStore = useUserStore()
let loginForm = reactive({
  username: 'admin',
  password: '111111',
})
let $router = useRouter()
let loading = ref(false)
let login = async () => {
  try {
    loading.value = true
    await userStore.userLogin(loginForm)
    $router.push('/')
    ElNotification({
      title: '小宝贝真聪明，还记得账号密码!',
      message: h(
        'i',
        { style: 'color: #008000' },
        localStorage.getItem('USERNAME') as string,
      ),
      type: 'success',
    })
    loading.value = false
  } catch (error) {
    ElNotification({
      title: '啥脑子啊，账号密码都忘了！',
      message: h('i', { style: 'color: #FF0000' }, (error as Error).message),
      type: 'error',
    })
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.login_box {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/login.png') no-repeat;
  background-size: cover;
}

.login_form {
  position: relative;
  width: 50%;
  top: 35vh;

  h1 {
    font-size: 30px;
    margin-bottom: 60px;
    font-weight: bold;
  }

  .register {
    display: flex;
    justify-content: space-between;
    font-size: 16px;
    margin: 30px 0 40px 0;
  }

  .butStyle {
    margin: 0 auto;
    width: 90%;
    height: 5vh;
    font-size: 24px;
  }
}
</style>
